<template>
  <div>
    <el-tree
      ref="tree"
      :data="perList"
      :props="{label:'name'}"
      show-checkbox
      node-key="id"
      default-expand-all
      check-strictly
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { tranListToTreeData } from '@/utils'
import { assignPrem, getrolsDetail } from '@/api/role'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      perList: [],
      permIds: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    hSubmit() {
      this.permIds = this.$refs.tree.getCheckedKeys()
      // console.log(this.permIds)
      assignPrem({ id: this.curId, permIds: this.permIds })
      this.$emit('close')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        if (res.code !== 10000) return this.$message.error(res.message)
        // console.log(res)
        this.perList = tranListToTreeData(res.data)
        const detail = await getrolsDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        // console.log(detail.data.permIds)
        // console.log(detail)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
